﻿using PagedList;
using PagedList.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GiaoTrinhEPU.Repositories.Repositories;
using GiaoTrinhEPU.Repositories.DataModels;
using GiaoTrinhEPU.Repositories;
using GiaoTrinhEPU.Repositories.Interfaces;
using GiaoTrinhEPU.Core.ActionResults;
using GiaoTrinhEPU.Core;

namespace GiaoTrinhEPU.Controllers
{
    public class DepartmentController : BaseController
    {
        /// <summary>
        /// Declare Department Repository
        /// </summary>
        private DepartmentRepository departmentRepository;

        /// <summary>
        ///  Declare Session
        /// </summary>
        public static string SessionDepartmentSort = "SessionDepartmentSort";
        public static string SessionDepartmentSearch = "SessionDepartmentSearch";
        public static string SessionDepartmentSortOpt = "SessionDepartmentSortOpt";



        // GET: /Department/
        /// <summary>
        /// Constructor that allow to create a Comment controller
        /// </summary>
        public DepartmentController()
            : base()
        {
            this.departmentRepository = new DepartmentRepository();
            ViewBag.SelectedMenuId = 25;
        }

        public ActionResult Index(int page = 1, int size = 25, String search = "", String opt = "", String sort = "")
        {
            ViewBag.Title = "Khoa";
            // Stored Session

            Session[SessionDepartmentSort] = sort.Trim().Length > 0 ? sort.ToLower() : Session[SessionDepartmentSort];
            Session[SessionDepartmentSortOpt] = opt.Trim().Length > 0 ? opt.ToLower() : Session[SessionDepartmentSortOpt];

            var department = this.departmentRepository.ListDepartments(false).ToList();

            // Search
            if (Session[SessionDepartmentSearch] != null)
            {
                ViewBag.Keyword = Session[SessionDepartmentSearch].ToString();

                department = department.Where(l => l.Name.ToUpper().Contains(ViewBag.Keyword.ToUpper())).ToList();

            }
            // Sort 
            ViewBag.SortNameOpt = "desc";
            if (Session[SessionDepartmentSort] != null)
            {
                switch (Session[SessionDepartmentSort].ToString())
                {
                    case "name":
                        department = Session[SessionDepartmentSortOpt].ToString().Equals("desc") ? department.OrderByDescending(s => s.Name).ToList() : department.OrderBy(s => s.Name).ToList();

                        Session[SessionDepartmentSort] = "name";
                        ViewBag.SortNameOpt = Sort.SetSortOrderOption(Session[SessionDepartmentSortOpt].ToString());
                        break;

                    default:
                        break;
                }
            }


            return View(department.ToPagedList(page, size));
        }
    }
}
        